<?
/***********************************************************
This file includes the default language for now, so that news
are generated in the server's default language. The news text
will have to be removed from the database for the next version
************************************************************/
if (!defined('SMF'))
	die('Hacking attempt...');

// Check for news related to the number of planets each player owns
$result = $smcFunc['db_query']('', '
	SELECT COUNT(owner) AS amount, owner
	FROM {raw:db_planets}
	WHERE owner != 0
	GROUP BY owner
	ORDER BY amount ASC',
	array(
		'db_planets' => $dbtables['planets']
	)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
	// If the player has 50 or more planets.
	if ($row['amount'] >= 50)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "planet50"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$planetcount = 50;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $planetcount . $txt['bnt_news_planets'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_p_text50']);
			
			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"planet50"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 25 or more planets.
	elseif ($row['amount'] >= 25)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "planet25"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$planetcount = 25;
			
			$name = get_player_name($row['owner']);
			
			$message .= 'bnt_news_p_headline: ' . $txt['bnt_news_p_headline'];
			$message .= 'bnt_news_planets: ' . $txt['bnt_news_planets'];
			$message .= $name;
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $planetcount . $txt['bnt_news_planets'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_p_text25']);
			
			$message .= $headline_insert . $news_insert;
			
			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"planet25"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 10 or more planets.
	elseif ($row['amount'] >= 10)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "planet10"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$planetcount = 10;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $planetcount . $txt['bnt_news_planets'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_p_text10']);
			
			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"planet10"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 5 or more planets.
	elseif ($row['amount'] >= 5)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "planet5"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$planetcount = 5;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $planetcount . $txt['bnt_news_planets'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_p_text5']);
			
			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"planet5"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
}
$smcFunc['db_free_result']($result);


// Check for news related to the number of colonists on all the planets each player owns.
$result = $smcFunc['db_query']('', '
	SELECT SUM(colonists) AS amount, owner
	FROM {raw:db_planets}
	WHERE owner != 0
	GROUP BY owner
	ORDER BY amount ASC',
	array(
		'db_planets' => $dbtables['planets']
	)
);

while ($row = $smcFunc['db_fetch_assoc']($result))
{
	// If the player has 1 Billion or more colonists
	if ($row['amount'] >= 1000000000)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "col1000"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$colcount = 1000;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $colcount . $txt['bnt_news_cols'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_c_text1000']);

			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"col1000"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 500 million or more colonists
	elseif ($row['amount'] >= 500000000)
	{
  		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "col500"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$colcount = 500;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $colcount . $txt['bnt_news_cols'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_c_text500']);

			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"col500"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 100 million or more colonist	
	elseif ($row['amount'] >= 100000000)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "col100"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$colcount = 100;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $colcount . $txt['bnt_news_cols'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_c_text100']);

			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"col100"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
	// If the player has 25 million or more colonist
	elseif ($row['amount'] >= 25000000)
	{
		$result2 = $smcFunc['db_query']('', '
			SELECT *
			FROM {raw:db_news}
			WHERE user_id = {int:owner}
			AND news_type = "col25"',
			array(
				'db_news' => $dbtables['news'],
				'owner' => $row['owner']
			)
		);
		
		// If the result is zero, this indicates a new news item
		if ($smcFunc['db_num_rows']($result2) == 0)
		{
			$colcount = 25;
			
			$name = get_player_name($row['owner']);
			
			$headline_insert = str_replace("[player]", $name, $txt['bnt_news_p_headline']);
			$headline_insert .= $colcount . $txt['bnt_news_cols'];
			
			$news_insert = str_replace("[name]", $name, $txt['bnt_news_c_text25']);

			$smcFunc['db_insert']('insert',
				$dbtables['news'],
				array(
					'headline' => 'string',
					'newstext' => 'string',
					'user_id' => 'int',
					'date' => 'string',
					'news_type' => 'string'
				),
				array(
					$headline_insert,
					$news_insert,
					$row['owner'],
					Now(),
					"col25"
				),
				''
			);
		}
		$smcFunc['db_free_result']($result2);
	}
}
$smcFunc['db_free_result']($result);
?>
